package com.spr.service;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.spr.exception.ScheduleNotFound;
import com.spr.model.Schedule;
import com.spr.repository.ScheduleRepository;

@Service
public class ScheduleServiceImpl implements ScheduleService{

	@Resource
    private ScheduleRepository ScheduleRepository;
	 
	@Override
   @Transactional
   public Schedule create(Schedule schedule) {
           Schedule createdschedule = schedule;
           return ScheduleRepository.save(createdschedule);
   }   
	
	@Override
   @Transactional
   public Schedule findById(int id) {
           return ScheduleRepository.findOne(id);
   }
	
	 @Override
    @Transactional(rollbackFor=ScheduleNotFound.class)
    public Schedule delete(int id) throws ScheduleNotFound {
            Schedule deletedschedule = ScheduleRepository.findOne(id);
            
            if (deletedschedule == null)
                    throw new ScheduleNotFound();
            
            ScheduleRepository.delete(deletedschedule);
            return deletedschedule;
    }

    @Override
    @Transactional
    public List<Schedule> findAll() {
            return ScheduleRepository.findAll();
    }

    @Override
    @Transactional(rollbackFor=ScheduleNotFound.class)
    public Schedule update(Schedule schedule) throws ScheduleNotFound {
            Schedule updatedSchedule = ScheduleRepository.findOne(schedule.getScheduleID());
            
            if (updatedSchedule == null)
                    throw new ScheduleNotFound();
            
            //Sunday
            updatedSchedule.setSundayFrom(schedule.getSundayFrom());
            updatedSchedule.setSundayTo(schedule.getSundayTo());
            
            //Monday
            updatedSchedule.setMondayFrom(schedule.getMondayFrom());
            updatedSchedule.setMondayTo(schedule.getMondayTo());
            
            //Tuesday
            updatedSchedule.setTuesdayFrom(schedule.getTuesdayFrom());
            updatedSchedule.setTuesdayTo(schedule.getTuesdayTo());
            
            //Wednesday
            updatedSchedule.setWednesdayFrom(schedule.getWednesdayFrom());
            updatedSchedule.setWednesdayTo(schedule.getWednesdayTo());
            
            //Thursday
            updatedSchedule.setThursdayFrom(schedule.getThursdayFrom());
            updatedSchedule.setThursdayTo(schedule.getThursdayTo());
            
            //Friday
            updatedSchedule.setFridayFrom(schedule.getFridayFrom());
            updatedSchedule.setFridayTo(schedule.getFridayTo());
            
            //Saturday
            updatedSchedule.setSaturdayFrom(schedule.getSaturdayFrom());
            updatedSchedule.setSaturdayTo(schedule.getSaturdayTo());
            

            return ScheduleRepository.save(updatedSchedule);
    }

}
